<?php
define('FANWE_ROOT',str_replace('\\', '/',getcwd()).'/');
$width    = isset($_GET['width']) ? intval($_GET['width']) : 0;
$height   = isset($_GET['height']) ? intval($_GET['height']) : 0;
$file     = $_GET['src'];

$thumb_url=getThumbImage($file,$width,$height);
header("Location: $thumb_url");
exit;

function getThumbImage($file,$width,$height)
{
	$uri = '';
	$hash_path = md5($file . $width . $height);
	$thumb_dir ='public/upload/temp_thumb/' . $hash_path{0} . $hash_path{1};
	$thumb_dir1 = $thumb_dir . '/' . $hash_path{2} . $hash_path{3};
	$thumb_path = $thumb_dir1 . '/' . $hash_path;

	if (function_exists('imagejpeg'))
	{
		$thumb_path .= '.jpg';
	}
	elseif (function_exists('imagegif'))
	{
		$thumb_path .= '.gif';
	}
	elseif (function_exists('imagepng'))
	{
		$thumb_path .= '.png';
	}
	else
	{
		returnSourceImg($img);
	}
	
	if (!is_file(FANWE_ROOT.$thumb_path))
	{
		if(!file_exists(FANWE_ROOT.$thumb_dir))
		{
			mkdir(FANWE_ROOT.$thumb_dir);
		}
		if(!file_exists(FANWE_ROOT.$thumb_dir1))
		{
			mkdir(FANWE_ROOT.$thumb_dir1);
		}
		if(!parseUrl($file))
			$file = FANWE_ROOT.$file;
			
		makeThumb($file,FANWE_ROOT.$thumb_path,$width,$height);
	}
	
	return $thumb_path;
}


function returnSourceImg($imgsrc)
{
	$src=str_replace(FANWE_ROOT, '', str_replace('\\', '/', realpath($imgsrc)));
	header("Location: $src");
	exit;
}

function makeThumb($img, $filename, $thumb_width = 0, $thumb_height = 0)
{
	 $gd = gdVersion(); 
	 
	 if ($gd == 0)
	 {
		 returnSourceImg($img);
	 }
	
	/* 检查缩略图宽度和高度是否合法 */
	if ($thumb_width == 0 && $thumb_height == 0)
	{
		returnSourceImg($img);
	}
	
	/* 检查原始文件是否存在及获得原始文件的信息 */
	$org_info = @getimagesize($img);
	
	if (!$org_info)
	{
		returnSourceImg($img);
	}
	
	if (!checkImgFunction($org_info[2]))
	{
		returnSourceImg($img);
	}
	
	$img_org = imgResource($img, $org_info[2]);
	
	/* 原始图片以及缩略图的尺寸比例 */
	$scale_org      = $org_info[0] / $org_info[1];
	
	/* 处理只有缩略图宽和高有一个为0的情况，这时背景和缩略图一样大 */
	if ($thumb_width == 0)
	{
		$thumb_width = $thumb_height * $scale_org;
	}
	if ($thumb_height == 0)
	{
		$thumb_height = $thumb_width / $scale_org;
	}
	
	/* 创建缩略图的标志符 */
	if ($gd == 2)
	{
		$img_thumb  = imagecreatetruecolor($thumb_width, $thumb_height);
	}
	else
	{
		$img_thumb  = imagecreate($thumb_width, $thumb_height);
	}
	
	$bgcolor ="FFFFFF";
	sscanf($bgcolor, "%2x%2x%2x", $red, $green, $blue);
	$clr = imagecolorallocate($img_thumb, $red, $green, $blue);
	imagefilledrectangle($img_thumb, 0, 0, $thumb_width, $thumb_height, $clr);
	
	if ($org_info[0] / $thumb_width < $org_info[1] / $thumb_height)
	{
		$lessen_width  = $thumb_width;
		$lessen_height  = $thumb_width / $scale_org;
	}
	else
	{
		$lessen_width  = $thumb_height * $org_info[0] / $org_info[1];
		$lessen_height  = $thumb_height;
	}
	
	$dst_x = ($thumb_width  - $lessen_width)  / 2;
	$dst_y = ($thumb_height - $lessen_height) / 2;
	
	/* 将原始图片进行缩放处理 */
	if ($gd == 2)
	{
		imagecopyresampled($img_thumb, $img_org, $dst_x, $dst_y, 0, 0, $lessen_width, $lessen_height, $org_info[0], $org_info[1]);
	}
	else
	{
		imagecopyresized($img_thumb, $img_org, $dst_x, $dst_y, 0, 0, $lessen_width, $lessen_height, $org_info[0], $org_info[1]);
	}
	
	/* 生成文件 */
	if (function_exists('imagejpeg'))
	{
		imagejpeg($img_thumb,$filename,100);
	}
	elseif (function_exists('imagegif'))
	{
		imagegif($img_thumb,$filename);
	}
	elseif (function_exists('imagepng'))
	{
		imagepng($img_thumb,$filename);
	}
	else
	{
		returnSourceImg($img);
	}
	
	imagedestroy($img_thumb);
	imagedestroy($img_org);
	
	//确认文件是否生成
	if (!file_exists($filename))
	{
		returnSourceImg($img);
	}
}

function gdVersion()
{
	static $gdVersion = -1;
	
	if ($gdVersion >= 0)
	{
		return $gdVersion;
	}
	
	if (!extension_loaded('gd'))
	{
		$gdVersion = 0;
	}
	else
	{
		// 尝试使用gd_info函数
		if (PHP_VERSION >= '4.3')
		{
			if (function_exists('gd_info'))
			{
				$ver_info = gd_info();
				preg_match('/\d/', $ver_info['GD Version'], $match);
				$gdVersion = $match[0];
			}
			else
			{
				if (function_exists('imagecreatetruecolor'))
				{
					$gdVersion = 2;
				}
				elseif (function_exists('imagecreate'))
				{
					$gdVersion = 1;
				}
			}
		}
		else
		{
			if (preg_match('/phpinfo/', ini_get('disable_functions')))
			{
				/* 如果phpinfo被禁用，无法确定gd版本 */
				$gdVersion = 1;
			}
			else
			{
			  // 使用phpinfo函数
			   ob_start();
			   phpinfo(8);
			   $info = ob_get_contents();
			   ob_end_clean();
			   $info = stristr($info, 'gd version');
			   preg_match('/\d/', $info, $match);
			   $gdVersion = $match[0];
			}
		 }
	}
	
	return $gdVersion;
}

function checkImgFunction($img_type)
{
	switch ($img_type)
	{
		case 'image/gif':
		case 1:
	
			if (PHP_VERSION >= '4.3')
			{
				return function_exists('imagecreatefromgif');
			}
			else
			{
				return (imagetypes() & IMG_GIF) > 0;
			}
		break;
	
		case 'image/pjpeg':
		case 'image/jpeg':
		case 2:
			if (PHP_VERSION >= '4.3')
			{
				return function_exists('imagecreatefromjpeg');
			}
			else
			{
				return (imagetypes() & IMG_JPG) > 0;
			}
		break;
	
		case 'image/x-png':
		case 'image/png':
		case 3:
			if (PHP_VERSION >= '4.3')
			{
				 return function_exists('imagecreatefrompng');
			}
			else
			{
				return (imagetypes() & IMG_PNG) > 0;
			}
		break;
	
		default:
			return false;
	}
}

function imgResource($img_file, $mime_type)
{
	switch ($mime_type)
	{
		case 1:
		case 'image/gif':
			$res = imagecreatefromgif($img_file);
			break;
	
		case 2:
		case 'image/pjpeg':
		case 'image/jpeg':
			$res = imagecreatefromjpeg($img_file);
			break;
	
		case 3:
		case 'image/x-png':
		case 'image/png':
			$res = imagecreatefrompng($img_file);
			break;
	
		default:
			return false;
	}
	
	return $res;
}

function parseUrl($url)
{
    $parse_url = @parse_url($url);
    return (!empty($parse_url['scheme']) && !empty($parse_url['host']));
}
?>